package com.woniu.service.impl;

import com.woniu.dao.CommunityFavorMapper;
import com.woniu.dao.DynamicCommentMapper;
import com.woniu.dao.DynamicMapper;
import com.woniu.dto.DynamicCommentDto;
import com.woniu.dto.DynamicPost;
import com.woniu.pojo.CommunityFavor;
import com.woniu.pojo.Dynamic;
import com.woniu.pojo.DynamicComment;
import com.woniu.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DynamicServiceImpl implements DynamicService {

    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private CommunityFavorMapper communityFavorMapper;
    @Autowired
    private DynamicCommentMapper dynamicCommentMapper;

    /**
     * 保存动态
     * @param map
     */
    @Override
    public void save(Map<String, Object> map) {
        //构造一个新的动态实体
        Integer userId = Integer.valueOf(map.get("userId").toString());
        String picPath = String.valueOf(map.get("picPath"));
        Integer type = Integer.valueOf(map.get("type").toString());
        Integer favor = Integer.valueOf(map.get("favor").toString());
        String content = String.valueOf(map.get("content"));
        Dynamic dynamic = new Dynamic(userId,picPath,type,favor,new Date(),content);
        dynamicMapper.insertSelective(dynamic);
    }

    /**
     * 获取动态
     * @param type
     * @param page
     * @return
     */
    @Override
    public List<DynamicPost> findByType(Integer type, Integer page,Integer userId) {
        //判断是否是查询全部类型,0:全部
        List<DynamicPost> dynamicPosts;
        if(type == 0){
             dynamicPosts = dynamicMapper.selectByPage(page,10);
        }else{
            dynamicPosts = dynamicMapper.selectByType(type,page,10);
        }

        //新增评论数量信息
        for(int i=0;i<dynamicPosts.size();i++){
            dynamicPosts.get(i).setCommentCount(dynamicCommentMapper.selectCountById(dynamicPosts.get(i).getId()));
        }
        //新增点赞数量信息
        for(int i=0;i<dynamicPosts.size();i++){
            dynamicPosts.get(i).setFavorCount(communityFavorMapper.selectCountById(dynamicPosts.get(i).getId()));
        }
        //如果用户登录了，则展示当前用户点赞状态
        if(userId!=null){
            //设置当前用户点赞状态
            //获取当前用户所有点赞动态
            List<Integer> list = communityFavorMapper.selectIdByUserId(userId);
            //遍历加载的新动态
            for(int i=0;i<dynamicPosts.size();i++){
                if(list.contains(dynamicPosts.get(i).getId())){
                    dynamicPosts.get(i).setFavorStatus(1);
                }
            }
        }
        return dynamicPosts;
    }

    /**
     * 根据动态id获取评论消息
     * @param dynamicId
     * @return
     */
    @Override
    public List<DynamicCommentDto> findByComment(Integer dynamicId) {
        List<DynamicCommentDto> dynamicCommentDtos = dynamicMapper.selectById(dynamicId);
        //查出是否是回复另一个人的评论
        for(int i=0;i<dynamicCommentDtos.size();i++){
            //另一个人id不为空，则该评论为回复其他人的评论
            if(dynamicCommentDtos.get(i).getCdcOtherUserId()!=null){
                for(int j=0;j<dynamicCommentDtos.size();j++){
                    //找到回复人，则将该回复人的名字设置进去
                    if(dynamicCommentDtos.get(i).getCdcOtherUserId()==dynamicCommentDtos.get(j).getCdcUserId()){
                        dynamicCommentDtos.get(i).setOtherName(dynamicCommentDtos.get(j).getName());
                        break;
                    }
                }
            }
        }
        return dynamicCommentDtos;
    }

    /**
     * 根据动态id获取点赞人消息
     * @param dynamicId
     * @return
     */
    @Override
    public List<CommunityFavor> findFavorById(Integer dynamicId) {
        return communityFavorMapper.selectById(dynamicId);
    }

    @Override
    public void saveFavor(Integer dynamicId, Integer userId) {
        //获取点赞人头像信息
        String picPath = dynamicMapper.selectUserPicById(userId);
        //new一个新的点赞实体
        CommunityFavor communityFavor = new CommunityFavor(dynamicId,userId,picPath,new Date());
        communityFavorMapper.insertSelective(communityFavor);
    }

    /**
     * 取消点赞
     * @param dynamicId
     * @param userId
     */
    @Override
    public void deleteFavor(Integer dynamicId, Integer userId) {
        communityFavorMapper.deleteFavorById(dynamicId,userId);
    }

    /**
     * 保存评论
     * @param map
     */
    @Override
    public void saveComment(Map<String, Object> map) {
        //获取必要信息
        Integer dynamicId = Integer.valueOf(map.get("dynamicId").toString());
        Integer userId = Integer.valueOf(map.get("userId").toString());
        Integer otherUserId = null;
        //如果是对其他人的评论进行回复，则必须写入其他人的用户id
        if(map.get("otherUserId")!=null){
            otherUserId = Integer.valueOf(map.get("otherUserId").toString());
        }
        String content = String.valueOf(map.get("content"));
        //新建一个评论实体
        DynamicComment dynamicComment = new DynamicComment(dynamicId,userId,otherUserId,content,new Date());
        dynamicCommentMapper.insertSelective(dynamicComment);
    }

    /**
     * 获取指定id 的动态信息
     * @param dynamicId
     * @return
     */
    @Override
    public DynamicPost findDynamicById(Integer dynamicId) {
        DynamicPost dynamicPost  = dynamicMapper.selectDynamicById(dynamicId);
        return dynamicPost;
    }

}
